class Solution {
public:
  bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {

    // 邻接矩阵
    vector<int> graph[numCourses];
    // 入度表
    vector<int> indegree(numCourses, 0);
    queue<int> q;
    bool ans = true;

    // 建邻接矩阵与入度表
    for(vector<int> &v : prerequisites){
      graph[v.at(1)].emplace_back(v.at(0));
      ++indegree.at(v.at(0));
    }
    for(int i = 0; i < indegree.size(); ++i){
      if(indegree.at(i) == 0){
        q.emplace(i);
      }
    }

    while(!q.empty()){
      int cur = q.front();
      q.pop();

      for(int &v : graph[cur]){
        --indegree.at(v);
        if(indegree.at(v) == 0){
          q.emplace(v);
        }
      }
    }

    for(int &v : indegree){
      if(v > 0){
        ans = false;
        break;
      }
    }

    return ans;

  }
};